﻿using System;
using System.Collections;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Threading;
using System.Windows;
namespace WPFExcelReport
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MMONP3 : Window
    {
        System.Timers.Timer bTimer = new System.Timers.Timer();
        public string RUNID = "";
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        string company
        {
            get
            {
                return app.company;
            }
        }
        BaseFunction INTEXDBC;

        bool IsRun = true;
        public MMONP3()
        {
            InitializeComponent();
            bTimer.Interval = 1000 * 60 * 60 * 4;
            bTimer.Start();
            bTimer.Elapsed += bTimer_Elapsed;
        }

        void bTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            KillMonp3();
        }
        Hashtable myHashtable = new Hashtable();
        private void KillMonp3()
        {
            CheckExcellProcesses();
            Process[] AllProcesses = Process.GetProcessesByName("MONP3");

            // check to kill the right process
            foreach (Process ExcelProcess in AllProcesses)
            {
                if (!myHashtable.ContainsKey(ExcelProcess.Id))
                    ExcelProcess.Kill();
            } 
            AllProcesses = null;
        }
        string connection = "";
        private void CheckExcellProcesses()
        {
            if (app.EnvExe == null)
            {
                app.Load();
            }
            connection = app.OracleDB.ConnectionString;
            if (INTEXDBC == null)
            {
                INTEXDBC = new BaseFunction();
                INTEXDBC.proxy = app.proxy;
                INTEXDBC.OracleDB = app.OracleDB;
            }
            DataSet SetProcess = new DataSet();
            INTEXDBC.proxy = app.proxy; 
            OracleCommand or1 = INTEXDBC.OracleDB.CreateCommand();
            or1.CommandText = "SELECT ITPPID  FROM ITPRS@intex2 union SELECT ITPPID  FROM ITPRS@intex1";
            DataSet SResult = INTEXDBC.Connect2Proxy(or1);
            SResult.Tables[0].TableName = "ITPRS";
            SetProcess.Merge(SResult); 
            Process[] AllProcesses = Process.GetProcessesByName("MONP3");
            myHashtable = new Hashtable();
            int iCount = 0; 
            foreach (Process ExcelProcess in AllProcesses)
            {
                int pid = ExcelProcess.Id;
                var query = from DataRow row in SetProcess.Tables[0].AsEnumerable()
                            where Convert.ToInt64(row["ITPPID"].ToString()) == pid
                            select row;
                foreach (DataRow row in query)
                {
                    myHashtable.Add(ExcelProcess.Id, iCount);
                    iCount = iCount + 1;
                }
            }
        }


        public void AutoRun()
        {
            if (!IsRun)
                return;
            KillMonp3();
           Thread.SpinWait(1000 * 60 * 60 * 4);
            // Thread.Sleep(1000 * 60);
            AutoRun();
        }

        private void btnKillMonp3_Click(object sender, RoutedEventArgs e)
        {

            AutoRun();
        }
    }
}
